#! /usr/bin/env python
#coding=utf-8

import io

class DeleteException(Exception):'exception:mapping has no item'

slash = '#'

class hostsdict(object):
    def __init__(self):
        self.__inndict = dict()
        
    def append(self, key, value):
        if key not in self.__inndict:
            self.__inndict[key] = [value]
        else:
            self.__inndict[key].append(value)
        
            
    def delete(self, key, value):
        if key not in self.__inndict:
            raise DeleteException()
        else:
            del(self.__inndict[key][self.__inndict[key].index(value)])
        
        if len(self.__inndict[key])==0:
            del(self.__inndict[key])
            

    def getSites(self, host):
        if host in self.__inndict:
            return tuple(self.__inndict[host])
        else:
            return None
    
    def items(self):
        return self.__inndict.items()
    
    def copy(self):
        return self.__inndict.copy()
        

class Hosts(object):
    def __init__(self):
        self.__PutawayMappings = hostsdict()
        self.__Mappings = hostsdict()
        self.__Header = []
        
    def Add(self, hostsSitesMaps):
        for hostaddr,site in hostsSitesMaps.items():
            if self.InPutwayMappings(hostaddr, site):
                self.RemoveFormPutwayMappings({hostaddr:site})
            else:
                self.__Mappings.append(hostaddr,site)
    
    
    def GetMappingsByHost(self, host):
        return self.__Mappings.getSites(host)
    
    def GetPutawayMappings(self, host):
        return self.__PutawayMappings.getSites(host)
    
    def Delete(self, hostsSiteMaps):
        for hostaddr, site in hostsSiteMaps.items():
            self.__Mappings.delete(hostaddr, site)
    
    def InPutwayMappings(self, hostaddr, site):
        return False

    def Putaway(self, hostsSiteMaps):
        self.Delete(hostsSiteMaps)
        for hostaddr,site in hostsSiteMaps.items():
            self.__PutawayMappings.append('#%s'%(hostaddr),site)

    
    def RemoveFromPutawayMapping(self, hostssitesMaps):
        pass
    
    def DeletePutawayMapping(self, hostsSiteMaps):
        for hostaddr, site in hostsSiteMaps.items():
            self.__PutawayMappings.delete(hostaddr, site)

    def GetAllMappings(self):
        return self.__Mappings.copy()
    
    def GetAllPutawayMappings(self):
        return self.__PutawayMappings.copy()
        
    def clear(self):
        self.__PutawayMappings = hostsdict()
        self.__Mappings = hostsdict()
        self.__Header = []
 
    def load(self, stream):
        try:
            for line in stream:
                #print line
                if line[0] != slash:
                    key,value = line.split()
                    self.__Mappings.append(key, value)
                else:
                    if line[1:3].isdigit():
                        key,value = line.split()
                        self.__PutawayMappings.append(key, value)
                    else:
                        self.__Header.append(line)
        except:
            pass
        return
    
    def save(self, stream):
        def writeHeader(stream, header):
            for line in header:
                stream.write('%s\n'%line)
                
        def writeMappings(stream, mappings):
            for host, sites in mappings.items():
                for site in sites:
                    stream.write('%s   %s\n'%(host, site))
        
        writeHeader(stream, self.__Header)
        writeMappings(stream, self.__Mappings)
        writeMappings(stream, self.__PutawayMappings)        
        stream.flush()
        

def open(filename):
    file = io.open(filename, 'r')
    hosts = Hosts()
    hosts.load(file)
    file.close()
    return hosts

def save(filename, hosts):
    file = io.open(filename, 'w')
    hosts.save(file)
    file.close()

    
